package com.souku.finance.application.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.souku.finance.application.R;
import com.souku.finance.application.bean.ExperienceListBean;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.widget.MyTextView;

import java.util.List;

/**
 * Created by Android on 2016/12/20.
 */

public class ExperienceAdapter extends BaseAdapter {

    private List<ExperienceListBean.ListBean> list;
    private Click click;

    public ExperienceAdapter(List<ExperienceListBean.ListBean> list, Click click) {
        this.list = list;
        this.click = click;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder = null;
        if (view == null || !(view.getTag() instanceof ViewHolder)) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_experience, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        final ExperienceListBean.ListBean bean = list.get(position);
        viewHolder.tvMoney.setText(MyTextUtil.setMoneyToElement(bean.getMoney()));
        viewHolder.tvDay.setText(bean.getDuration());
        viewHolder.tvPoint.setText(bean.getRate() + "%");
        viewHolder.tvName.setText(bean.getName());
        viewHolder.tvDate.setText(MyTextUtil.changeTimeFormat2(bean.getGmtCreated()));
        if (bean.getState().equals("1")) {
            viewHolder.btnOk.setText("使用");
            viewHolder.btnOk.setBackgroundResource(R.drawable.solid_red_round);
            viewHolder.btnOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    click.use(bean.getId());
                }
            });
        } else {
            viewHolder.btnOk.setOnClickListener(null);
            if (bean.getState().equals("2")) {
                viewHolder.btnOk.setText("使用中");
                viewHolder.btnOk.setBackgroundResource(R.drawable.solid_main_round);
            } else {
                viewHolder.btnOk.setText("已使用");
                viewHolder.btnOk.setBackgroundResource(R.drawable.solid_gray_round);
            }
        }
        return view;
    }


    static class ViewHolder {
        protected MyTextView tvMoney;
        protected Button btnOk;
        protected MyTextView tvPoint;
        protected MyTextView tvDay;
        protected TextView tvName;
        protected TextView tvDate;

        ViewHolder(View rootView) {
            initView(rootView);
        }

        private void initView(View rootView) {
            tvMoney = (MyTextView) rootView.findViewById(R.id.tv_money);
            btnOk = (Button) rootView.findViewById(R.id.btn_ok);
            tvPoint = (MyTextView) rootView.findViewById(R.id.tv_point);
            tvDay = (MyTextView) rootView.findViewById(R.id.tv_day);
            tvName = (TextView) rootView.findViewById(R.id.tv_name);
            tvDate = (TextView) rootView.findViewById(R.id.tv_date);
        }
    }

    public interface Click {
        void use(String id);
    }
}
